package br.com.unirio.autocar.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import br.com.unirio.autocar.modelo.dto.DTO;
import br.com.unirio.autocar.servicos.ParseDate;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class GeradorDePdf
{

    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);

    public static void geraPdf(String tituloDocumento, String caminho, DTO dto)
	    throws FileNotFoundException,
		DocumentException
    {
	try
	{
	    Document document = new Document(PageSize.LETTER.rotate());
	    PdfWriter.getInstance(
		    document,
			new FileOutputStream(caminho
				+ tituloDocumento
				    + "_"
				    + ParseDate.converterDataParaRelatorio()
				    + ".pdf"));
	    document.open();
	    addMetaData(document);
	    addContent(document, tituloDocumento, dto);
	    document.close();
	}
	catch (Exception e)
	{
	    e.printStackTrace();
	}
    }

    private static void addMetaData(Document document)
    {
	document.addAuthor("AutoCar");
    }

    private static void addContent(Document document, String tituloDocumento, DTO dto) throws DocumentException
    {
	Chapter catPart = new Chapter(new Paragraph(tituloDocumento, catFont), 1);

	addEmptyLine(catPart, 3);

	// add a table
	PdfPTable tabela = createTable(dto);
	PdfPTable tabelaTotal = createTableTotal(dto);

	// now add all this to the document
	document.add(catPart);
	document.add(tabela);
	addEmptyLine(catPart, 3);
	document.add(tabelaTotal);

    }

    private static PdfPTable createTableTotal(DTO dto) throws BadElementException
    {

	PdfPTable table = new PdfPTable(2);

	// table.setBorderColor(BaseColor.GRAY);
	// t.setPadding(4);
	// t.setSpacing(4);
	// t.setBorderWidth(1);
	PdfPCell c = new PdfPCell(new Phrase("VALOR TOTAL"));
	c.setBackgroundColor(BaseColor.LIGHT_GRAY);
	table.addCell(c);
	PdfPCell valor = new PdfPCell(new Phrase("R$ " + dto.getValorTotal().toString()));
	valor.setHorizontalAlignment(Element.ALIGN_RIGHT);
	table.addCell(valor);
	return table;

    }

    private static PdfPTable createTable(DTO dto) throws BadElementException
    {
	int numeroLinhas = dto.getColunas().length;

	PdfPTable table = new PdfPTable(numeroLinhas);

	// table.setBorderColor(BaseColor.GRAY);
	// t.setPadding(4);
	// t.setSpacing(4);
	// t.setBorderWidth(1);

	String[] nomeColunas = dto.getColunas();
	for (String coluna : nomeColunas)
	{
	    PdfPCell c = new PdfPCell(new Phrase(coluna));
	    c.setBackgroundColor(BaseColor.GRAY);

	    c.setHorizontalAlignment(Element.ALIGN_CENTER);
	    table.addCell(c);
	}
	table.setHeaderRows(1);

	for (DTO dtoFilho : dto.getFilhos())
	{
	    String[] valores = dtoFilho.getValores();
	    for (String valor : valores)
	    {
		table.addCell(valor);
	    }

	}

	return table;

    }

    private static void addEmptyLine(Chapter chapter, int number)
    {
	for (int i = 0; i < number; i++)
	{
	    chapter.add(new Paragraph(" "));
	}
    }
}
